﻿@using ZBJF.Warranty.Common.Enums.InsuranceOrder
@using ZBJF.Warranty.WebProvider

@{
    ViewBag.Title = "";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@using (Html.BeginForm("ImportInsuranceWarrantyRecordData", "InsuranceWarrantyRecord", FormMethod.Post, new { id = "importInsuranceWarrantyRecordForm", name = "importInsuranceWarrantyRecordForm", @class = "breadcrumb form-search", enctype = "multipart/form-data" }))
{

    <table>
        <tr>
            <td>
                <label>选择文件:</label>
            </td>
            <td>
                <span class="showFileName"></span>
                <a href="javascript:void(0)" class="file">
                    选择对账文件
                    <input type="file" name="importFile" validate="{required:true,messages:{required:'请先选择要上传的文件'}}" />
                </a>
                <input id="btnSubmit" class="btn btn-primary Import-R" type="submit" value="上传对账清单">
                <a href="/DownLoad/Template/ReceiptTemplate.xlsx">下载对账模版</a>
            </td>
        </tr>
        <tr>
            <td></td>
            <td></td>
        </tr>
    </table>
}
@if (!string.IsNullOrEmpty(ViewBag.ReturnInfo != null ? ViewBag.ReturnInfo.ToString() : string.Empty))
{
    <div style="margin-left: 50px;">
        <h3>对账结果</h3>
        @Html.Raw(ViewBag.ReturnInfo)
    </div>    
}
<div style="margin-top: 30%; text-align: center;">
    @if (!string.IsNullOrEmpty(ViewBag.ReturnInfo != null ? ViewBag.ReturnInfo.ToString() : string.Empty))
{
    <input id="btnSubmit" class="btn btn-primary  Search-R margin-l-10" type="button" value="下载对账结果清单" onclick="ExportData('@ViewBag.List')">
}
    <input id="btnColse" class="btn btn-primary  Search-R margin-l-10" type="button" value="关闭 " onclick="parent.layer.closeAll(); parent.location.reload();">
</div>
@section scripts{
    <script type="text/javascript">
    $(function() {
        $(".file").on("change", "input[type='file']", function() {
            var filePath = $(this).val();
            if (filePath.indexOf("xls") != -1 || filePath.indexOf("xlsx") != -1) {
                var arr = filePath.split('\\');
                var fileName = arr[arr.length - 1];
                $(".showFileName").html(fileName).removeClass("red");
            } else {
                $(".showFileName").html("请选择Excel文件").addClass("red");
                return;
            }
        });
        $("#importInsuranceWarrantyRecordForm").submit(function () {
            if ($(this).valid()) {
                if ($(".showFileName").hasClass("red")) {
                    layer.alert("请选择Excel文件");
                    return false;
                }
                layer.load();
                return true;
            }
            return false;
        });
    })

    function ExportData(tableJson) {
        /*导出数据*/
        if (!confirm("确定要导出Excel吗？")) return;
        if (tableJson == '')
                    return;
                debugger;
                JSONToExcelConvertor(tableJson, "Report");
    }
    function JSONToExcelConvertor(JSONData, FileName) {
        //先转化json
        var excelFile = "<html xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:x='urn:schemas-microsoft-com:office:excel' xmlns='http://www.w3.org/TR/REC-html40'>";
        excelFile += '<meta http-equiv="content-type" content="application/vnd.ms-excel; charset=UTF-8">';
        excelFile += '<meta http-equiv="content-type" content="application/vnd.ms-excel';
        excelFile += '; charset=UTF-8">';
        excelFile += "<head>";
        excelFile += "<!--[if gte mso 9]>";
        excelFile += "<xml>";
        excelFile += "<x:ExcelWorkbook>";
        excelFile += "<x:ExcelWorksheets>";
        excelFile += "<x:ExcelWorksheet>";
        excelFile += "<x:Name>";
        excelFile += "{worksheet}";
        excelFile += "</x:Name>";
        excelFile += "<x:WorksheetOptions>";
        excelFile += "<x:DisplayGridlines/>";
        excelFile += "</x:WorksheetOptions>";
        excelFile += "</x:ExcelWorksheet>";
        excelFile += "</x:ExcelWorksheets>";
        excelFile += "</x:ExcelWorkbook>";
        excelFile += "</xml>";
        excelFile += "<![endif]-->";
        excelFile += "</head>";
        excelFile += "<body>";
        excelFile += JSONData;
        excelFile += "</body>";
        excelFile += "</html>";


        var uri = 'data:application/vnd.ms-excel;charset=utf-8,' + encodeURIComponent(excelFile);

        var link = document.createElement("a");
        link.href = uri;

        link.style = "visibility:hidden";
        link.download = FileName + ".xls";

        document.body.appendChild(link);
        link.click();
        document.body.removeChild(link);
    }
    </script>
}